﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Generic;
using NIIT.SaoBacDau.Aupi.HospitalManagement.Entities;

namespace NIIT.SaoBacDau.Aupi.HospitalManagement.DataAccess
{
    public class DAO_Patient
    {
        public static Obj_Patient PtID()
        {
            SqlDataAdapter sqlda = new SqlDataAdapter("Patient.PatientID", DAO_ConnectDatabase.Connect());
            sqlda.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataTable dtable = new DataTable();
            sqlda.Fill(dtable);
             Obj_Patient pt = new Obj_Patient();
             pt.PatientID = dtable.Rows.Count;
            return pt;
        }

        public static bool AddQPT(Obj_Patient pt)
        {
            bool ErrorExcute = false;
            SqlConnection sc = DAO_ConnectDatabase.Connect();
            try
            {
                sc.Open();
                SqlCommand sqlcom = new SqlCommand("Patient.AddQuickPatient", sc);
                sqlcom.CommandType = CommandType.StoredProcedure;
                sqlcom.Parameters.Add("@PatientID", SqlDbType.Int).Value = pt.PatientID;
                sqlcom.Parameters.Add("@HospitalizedDate", SqlDbType.NVarChar).Value = pt.HospitalizedDate.ToString();
                sqlcom.Parameters.Add("@OrganizationDetailsID", SqlDbType.Int).Value = pt.OrganizationDetailsID;
                sqlcom.Parameters.Add("@States", SqlDbType.NVarChar).Value = pt.States;
                sqlcom.Parameters.Add("@Gender", SqlDbType.Bit).Value = pt.Gender;
                int SqlError = sqlcom.ExecuteNonQuery();
                if (SqlError == 0)
                    ErrorExcute = true;
                else if (SqlError == 1)
                    return ErrorExcute;
                return ErrorExcute;
            }
            finally
            {
                if (sc.State == ConnectionState.Open)
                    sc.Close();
            }
        }
        private static void Command(Obj_Patient patient, SqlCommand sqlcom)
        {
            sqlcom.CommandType = CommandType.StoredProcedure;
            sqlcom.Parameters.Add("@PatientID", SqlDbType.Int).Value = patient.PatientID;
            sqlcom.Parameters.Add("@HospitalizedDate", SqlDbType.NVarChar).Value = patient.HospitalizedDate.ToString();
            sqlcom.Parameters.Add("@States", SqlDbType.NVarChar).Value = patient.States;
            sqlcom.Parameters.Add("@Gender", SqlDbType.Bit).Value = patient.Gender;
            sqlcom.Parameters.Add("@Name", SqlDbType.NVarChar).Value = patient.Name;
            sqlcom.Parameters.Add("@BirthDay", SqlDbType.NVarChar).Value = patient.BirthDay.ToString();
            sqlcom.Parameters.Add("@TypePatient", SqlDbType.NVarChar).Value = patient.TypePatient;
            sqlcom.Parameters.Add("@TypeTreatment", SqlDbType.NVarChar).Value = patient.TypeTreatment;
            sqlcom.Parameters.Add("@Biography", SqlDbType.NVarChar).Value = patient.Biography;
            sqlcom.Parameters.Add("@HouseID", SqlDbType.NVarChar).Value = patient.HouseID;
            sqlcom.Parameters.Add("@Streets", SqlDbType.NVarChar).Value = patient.Stress;
            sqlcom.Parameters.Add("@Ward", SqlDbType.NVarChar).Value = patient.Ward;
            sqlcom.Parameters.Add("@District", SqlDbType.NVarChar).Value = patient.District;
            sqlcom.Parameters.Add("@City", SqlDbType.NVarChar).Value = patient.City;
            sqlcom.Parameters.Add("@PhoneNumber", SqlDbType.NVarChar).Value = patient.PhoneNumber;
            sqlcom.Parameters.Add("@Ethnic", SqlDbType.NVarChar).Value = patient.Ethnic;
            sqlcom.Parameters.Add("@AddressEmail", SqlDbType.NVarChar).Value = patient.AddressEmail;
            sqlcom.Parameters.Add("@Occupation", SqlDbType.NVarChar).Value = patient.Occupation;
            sqlcom.Parameters.Add("@AddressOccupation", SqlDbType.NVarChar).Value = patient.AddressOccupation;
            sqlcom.Parameters.Add("@ContactPerson", SqlDbType.NVarChar).Value = patient.ContactPerson;
            sqlcom.Parameters.Add("@ContactAddress", SqlDbType.NVarChar).Value = patient.ContactAddress;
            sqlcom.Parameters.Add("@ContactPhone", SqlDbType.NVarChar).Value = patient.ContactPhone;
        }
        public static bool AddPatient(Obj_Patient patient,int typeaddpatient)
        {
            bool ErrorExcute = false;
            SqlConnection sc = DAO_ConnectDatabase.Connect();
            SqlCommand sqlcom;
            string Procedure;
            if (typeaddpatient == 1)
                Procedure = "Patient.AddPatient1";
            else if (typeaddpatient == 2)
                Procedure = "Patient.AddPatient2";
            else if (typeaddpatient == 3)
                Procedure = "Patient.AddPatient3";
            else
                Procedure = "Patient.AddPatient";
            try
            {
                sc.Open();
                sqlcom = new SqlCommand(Procedure, sc);
                Command(patient, sqlcom);
                if (typeaddpatient == 1)
                {
                    sqlcom.Parameters.Add("@BedsID", SqlDbType.Int).Value = patient.BedsID;
                }
                else if (typeaddpatient == 2)
                {
                    sqlcom.Parameters.Add("@InsuranceID", SqlDbType.NVarChar).Value = patient.Cardnumber;
                }
                else if (typeaddpatient == 3){}
                else
                {
                    sqlcom.Parameters.Add("@InsuranceID", SqlDbType.NVarChar).Value = patient.Cardnumber;
                    sqlcom.Parameters.Add("@BedsID", SqlDbType.Int).Value = patient.BedsID;
                }
                int SqlError = sqlcom.ExecuteNonQuery();
                if (SqlError == 0)
                    ErrorExcute = true;
                else if (SqlError == 1)
                    return ErrorExcute;
                return ErrorExcute;
            }
            finally
            {
                if (sc.State == ConnectionState.Open)
                    sc.Close();
            }
        }

        public static List<Obj_Patient> ListObjPatient(int value, int to)
        {
            SqlDataAdapter sqlda = new SqlDataAdapter("Report.ListPatient", DAO_ConnectDatabase.Connect());
            sqlda.SelectCommand.CommandType = CommandType.StoredProcedure;
            sqlda.SelectCommand.Parameters.Add("@value", SqlDbType.Int).Value = value;
            sqlda.SelectCommand.Parameters.Add("@to", SqlDbType.Int).Value = to;
            DataTable dtable = new DataTable();
            sqlda.Fill(dtable);
            List<Obj_Patient> list = new List<Obj_Patient>();
            foreach (DataRow dr in dtable.Rows)
            {
                Obj_Patient patient = new Obj_Patient();
                patient.PatientID = Convert.ToInt32(dr["PatientID"].ToString());
                patient.OrganizationDetailsID = Convert.ToInt32(dr["OrganizationDetailsID"].ToString());
                patient.HospitalizedDate = dr["HospitalizedDate"].ToString();
                patient.States = dr["States"].ToString();
                patient.Gender = Convert.ToBoolean(dr["Gender"].ToString());
                patient.Name = dr["Name"].ToString();
                patient.BirthDay = dr["BirthDay"].ToString();
                patient.TypePatient = dr["TypePatient"].ToString();
                patient.Cardnumber = dr["InsuranceID"].ToString();
                patient.TypeTreatment = dr["TypeTreatment"].ToString();
                patient.BedsID = dr["BedsID"].ToString();
                patient.Biography = dr["Biography"].ToString();
                patient.HouseID = dr["HouseID"].ToString();
                patient.Stress = dr["Streets"].ToString();
                patient.Ward = dr["Ward"].ToString();
                patient.District = dr["District"].ToString();
                patient.City = dr["City"].ToString();
                patient.PhoneNumber = dr["PhoneNumber"].ToString();
                patient.Ethnic = dr["Ethnic"].ToString();
                patient.AddressEmail = dr["AddressEmail"].ToString();
                patient.Occupation = dr["Occupation"].ToString();
                patient.ContactPerson = dr["ContactPerson"].ToString();
                list.Add(patient);
            }
            return list;
        }

        public static bool EditPatient(Obj_Patient patient, int typeaddpatient)
        {
            bool ErrorExcute = false;
            SqlConnection sc = DAO_ConnectDatabase.Connect();
            SqlCommand sqlcom;
            string Procedure;
            if (typeaddpatient == 1)
                Procedure = "Patient.EditPatient1";
            else if (typeaddpatient == 2)
                Procedure = "Patient.EditPatient2";
            else if (typeaddpatient == 3)
                Procedure = "Patient.EditPatient4";
            else
                Procedure = "Patient.EditPatient3";
            try
            {
                sc.Open();
                sqlcom = new SqlCommand(Procedure, sc);
                Command(patient, sqlcom);
                if (typeaddpatient == 1)
                {
                    sqlcom.Parameters.Add("@BedsID", SqlDbType.Int).Value = patient.BedsID;
                }
                else if (typeaddpatient == 2)
                {
                    sqlcom.Parameters.Add("@InsuranceID", SqlDbType.NVarChar).Value = patient.Cardnumber;
                }
                else if (typeaddpatient == 3) { }
                else
                {
                    sqlcom.Parameters.Add("@InsuranceID", SqlDbType.NVarChar).Value = patient.Cardnumber;
                    sqlcom.Parameters.Add("@BedsID", SqlDbType.Int).Value = patient.BedsID;
                }
                int SqlError = sqlcom.ExecuteNonQuery();
                if (SqlError == 0)
                    ErrorExcute = true;
                else if (SqlError == 1)
                    return ErrorExcute;
                return ErrorExcute;
            }
            finally
            {
                if (sc.State == ConnectionState.Open)
                    sc.Close();
            }
        }

        public static Obj_Patient ObjPatient(Obj_Patient patients)
        {
            SqlDataAdapter sqlda = new SqlDataAdapter("Patient.EditPatient", DAO_ConnectDatabase.Connect());
            sqlda.SelectCommand.CommandType = CommandType.StoredProcedure;
            sqlda.SelectCommand.Parameters.Add("@PatientID", SqlDbType.Int).Value = patients.PatientID;
            DataTable dtable = new DataTable();
            sqlda.Fill(dtable);
            Obj_Patient patient = Patient(dtable);
            return patient;

        }

        public static Obj_Patient SearchPatient(int p, Obj_Patient patients)
        {
            string Procerdure;
            if (p == 0)
                Procerdure = "Patient.SearchPatient1";
            else 
                Procerdure = "Patient.SearchPatient2";

            SqlDataAdapter sqlda = new SqlDataAdapter(Procerdure, DAO_ConnectDatabase.Connect());
            sqlda.SelectCommand.CommandType = CommandType.StoredProcedure;
            if (p == 0)
                sqlda.SelectCommand.Parameters.Add("@PatientID", SqlDbType.Int).Value = patients.PatientID;
            else if (p == 1)
                sqlda.SelectCommand.Parameters.Add("@InsuranceID", SqlDbType.NVarChar).Value = patients.Cardnumber;
            DataTable dtable = new DataTable();
            sqlda.Fill(dtable);
            Obj_Patient patient = Patient(dtable);
            return patient;
        }

        private static Obj_Patient Patient(DataTable dtable)
        {
            Obj_Patient patient = new Obj_Patient();
            if (dtable.Rows.Count == 0)
                patient = null;
            else
            {
                patient.PatientID = Convert.ToInt32(dtable.Rows[0]["PatientID"].ToString());
                patient.OrganizationDetailsID = Convert.ToInt32(dtable.Rows[0]["OrganizationDetailsID"].ToString());
                patient.HospitalizedDate = dtable.Rows[0]["HospitalizedDate"].ToString();
                patient.States = dtable.Rows[0]["States"].ToString();
                patient.Gender = Convert.ToBoolean(dtable.Rows[0]["Gender"].ToString());
                patient.Name = dtable.Rows[0]["Name"].ToString();
                patient.BirthDay = dtable.Rows[0]["BirthDay"].ToString();
                patient.TypePatient = dtable.Rows[0]["TypePatient"].ToString();
                patient.Cardnumber = dtable.Rows[0]["InsuranceID"].ToString();
                patient.TypeTreatment = dtable.Rows[0]["TypeTreatment"].ToString();
                patient.BedsID = dtable.Rows[0]["BedsID"].ToString();
                patient.Biography = dtable.Rows[0]["Biography"].ToString();
                patient.HouseID = dtable.Rows[0]["HouseID"].ToString();
                patient.Stress = dtable.Rows[0]["Streets"].ToString();
                patient.Ward = dtable.Rows[0]["Ward"].ToString();
                patient.District = dtable.Rows[0]["District"].ToString();
                patient.City = dtable.Rows[0]["City"].ToString();
                patient.PhoneNumber = dtable.Rows[0]["PhoneNumber"].ToString();
                patient.Ethnic = dtable.Rows[0]["Ethnic"].ToString();
                patient.AddressEmail = dtable.Rows[0]["AddressEmail"].ToString();
                patient.Occupation = dtable.Rows[0]["Occupation"].ToString();
                patient.AddressOccupation = dtable.Rows[0]["AddressOccupation"].ToString();
                patient.ContactPerson = dtable.Rows[0]["ContactPerson"].ToString();
                patient.ContactAddress = dtable.Rows[0]["ContactAddress"].ToString();
                patient.ContactPhone = dtable.Rows[0]["ContactPhone"].ToString();
            }
            return patient;
        }
    }
}
